﻿<%@ Page Title="" Language="C#" MasterPageFile="~/privado/masterpage/MasterPrivado.Master" AutoEventWireup="true" CodeBehind="GestionarCliente.aspx.cs" Inherits="HMS.PresentationWeb.privado.GestionarCliente" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">

        $(function () {
            fn_init();

        });
        function fn_init() {
            fn_buildtable();
            fn_filltable();
        }
        function fn_rowedit(gr) {
            var row = $('#tbGrid').getRowData(gr);
            window.location.href = "GestionarClienteGuardar.aspx?q=" + row.hidden;
        }
        function fn_buildtable() {

            try {
                $("#tbGrid").jqGrid({
                    datatype: 'local',
                    colNames: ['', '#', 'Nombres', 'Apellidos', 'DNI', 'RUC', 'Fecha Nacimiento', 'Numero de Tarjeta', 'Nombre de Tarjeta','Estado', 'Action'],
                    colModel: [
                           { name: 'hidden', index: 'hidden', hidden: true },
                           { name: 'row', index: 'row', width: 5, align: 'right', sorttype: 'int' },
                           { name: 'nombre', index: 'nombre', width: 30, align: "left", sorttype: 'text' },
                           { name: 'apellidos', index: 'apellidos', width: 30, align: "left", sorttype: 'text' },
                           { name: 'dni', index: 'dni', width: 25, align: "right", sorttype: 'text' },
                           { name: 'ruc', index: 'ruc', width: 30, align: "right", sorttype: 'text' },
                           { name: 'fechanac', index: 'fechanac', width: 30, align: "center", sorttype: 'date' },
                           { name: 'numtarjeta', index: 'numtarjeta', width: 30, align: "center", sorttype: 'text' },
                           { name: 'nomtarjeta', index: 'nomtarjeta', width: 20, align: 'center', sorttype: 'text' },
                           { name: 'estado', index: 'estado', width: 20, align: 'center' },
                           { name: 'action', index: 'action', width: 20, align: 'center' }
                    ],
                    width: 900,
                    height: 'auto',
                    pager: '#tbGridPager',
                    viewrecords: true,
                    sortorder: 'desc',
                    rowNum: 20,
                    rowList: [20, 40, 60],
                    caption: "Clientes",
                    multiselect: true
                });

            } catch (e) {
                fn_message('e', 'An error ocurred while loading grid...')
            }
        }

        function fn_filltable() {

            var glancedata = $("#<%=hfData.ClientID%>").val() != "" ? $("#<%=hfData.ClientID%>").val() : "{}";
            try {
                var obj = $.parseJSON(glancedata);
                var len = obj.length;
                for (var i = 0; i < len; i++) {
                    var index = i + 1;
                    var row = {
                        hidden: obj[i].Id,
                        row: index,
                        nombre: obj[i].Nombre,
                        apellidos: obj[i].Apellidos,
                        dni: obj[i].DNI,
                        ruc: obj[i].RUC,
                        fechanac: obj[i].FechaNacimiento,
                        numtarjeta: obj[i].NumCuenta,
                        nomtarjeta: obj[i].NombreCuenta,
                        estado: obj[i].Estado == 1 ? 'Habilitado' : 'InHabilitado',
                        action: "<input type='button' class='edit-button' title='Edit' onclick='fn_rowedit(" + index + ")' />"
                    };
                    jQuery("#tbGrid").jqGrid('addRowData', i + 1, row);
                }
                $("#tbGrid").trigger("reloadGrid");
            }
            catch (e) {
                fn_message('e', 'An error occurred while loading data...');
            }
        }/*
        function fn_delete() {
            var obj = { ID: "" };
            var lista = [];

            var selIndex = $("#tbGrid").getGridParam('selarrrow');
            var len = selIndex.length;
            var selIndex2 = $.extend(true, {}, selIndex);
            if (selIndex.length > 0) {
                for (var i = 0; i < selIndex.length; i++) {
                    var data = $('#tbGrid').getRowData(selIndex[i]);
                    obj.ID = data.hidden;
                    lista[i] = $.extend(true, {}, obj);
                }

                var json = JSON.stringify(lista);

                var senddata = '{ jsondata:"' + fn_jsonreplace(json) + '" }';
                var success = function (asw) {
                    if (asw.d == "Deleted successfully") {
                        fn_message('s', 'Deleted successfully');
                        for (var y = 0; y < len; y++) {
                            $("#tbGrid").delRowData(selIndex2[y]);
                        }
                    }
                    else
                        fn_message('e', 'Unable to delete the record(s)');
                };
                var error = function (xhr, ajaxOptions, thrownError) {
                    fn_message('e', 'An error occurred while sending data');
                };

                fn_callmethod("GestionarClientes.aspx/SendDelete", senddata, success, error);
            }
            else
                fn_message('i', "Please select at least one row to delete");
        }
        */
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div>
            <h2>
                Clientes
            </h2>
    </div>
    <div>
        <ul>
            <li title="Agregar Cliente"><a href="GestionarClienteGuardar.aspx" id="ag_cliente"><span></span><span>Agregar</span></a></li>
        </ul>
        <ul>
            <li title="Eliminar Cliente"><a onclick="fn_delete()" id="el_cliente"><span>
            </span><span>Eliminar</span></a></li>
        </ul>
    </div>
    <div>
        <table id="tbGrid">
        </table>
        <div id="tbGridPager">
        </div>
    </div>
    <asp:HiddenField ID="hfData" runat="server" />
</asp:Content>
